﻿using System;
using System.Runtime.InteropServices;
using DirectShowLib;
using MediaFoundation.EVR;

namespace MediaPlayer.Renderers
{
    public abstract class Basic : IDisposable
    {
        internal IntPtr handle;
        internal IBaseFilter filter;

        public IVMRWindowlessControl Control { get; internal set; }

        public Basic(IntPtr handle)
        {
            this.handle = handle;
        }

        public virtual void SetVideoPosition(MFVideoNormalizedRect src, DsRect dst) { }
        public virtual void DisplayModeChanged() { }
        public virtual void Repaint(IntPtr hdc) { }

        protected virtual void Dispose(Boolean freeObjects)
        {
            try
            {
                if (!freeObjects) return;

                lock (this)
                {
                    Player.Graph.RemoveFilter(this.filter);
                    Marshal.ReleaseComObject(this.filter);
                    this.filter = null;
                   
                    this.Control = null;
                }
            }
            finally
            {
                GC.Collect();
            }
        }

        public virtual void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        ~Basic()
        {
            Dispose(false);
        }
    }
}
